package com.xb.loan.encryptor.classloader;


import com.xb.loan.encryptor.util.LogFormat;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import org.slf4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 可以配置多个socket信息 方式 name.prop
 * 
 * @author lottery
 * 
 */
public class ClassLoaderConfig {

	private String serverIp;// 服务器ip地址
	private String port; // 连接端口
	private String idleTime;// 空闲时间，超过空闲时间发送心跳
	private String sotimeout; // 读取数据超时时间
	private String localEncoding;// 本地字符集编码
	private String remoteEncoding;// 服务器字符集
	private String guid;//唯一码
	private static Map<String, ClassLoaderConfig> instances = new HashMap<String, ClassLoaderConfig>();

	private static Logger logger = DefaultLoggerFacotry.getLogger(ClassLoaderConfig.class);

	static {

		init();

	}

	private static void init() {

		String name = "config/classloaderconfig.properties";

		Properties prop = readAsProperties(name);

		if (prop != null) {

			try {

				Set<Object> keys = prop.keySet();

				for (Object object : keys) {

					String[] keyData = object.toString().split("\\.");

					Field field = ClassLoaderConfig.class
							.getDeclaredField(keyData[1]);

					ClassLoaderConfig config = instances.get(keyData[0]);

					if (config == null) {

						config = new ClassLoaderConfig();
						logger.info(LogFormat.formatMsg("SocketConfig.init",
								"SocketConfig.init",
								"put new config to instances,name:"
										+ keyData[0]));
						instances.put(keyData[0], config);
					}

					field.set(config, prop.get(object));
				}

			} catch (Exception e) {

				logger.error(LogFormat.formatMsg("SocketConfig.init",
						"SocketConfig.init", "init has exception."), e);
			}

		} else {

			logger.info(LogFormat.formatMsg("SocketConfig.init",
					"SocketConfig.init", "read from file false,file:" + name));
		}

	}

	private static Properties readAsProperties(String name) {

		InputStream in = null;
		Properties prop = null;
		try {
			in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);

			prop = new Properties();

			prop.load(in);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {

			if (in != null) {

				try {

					in.close();
				} catch (IOException e) {

					e.printStackTrace();
				}
			}

		}

		return prop;
	}

	public static ClassLoaderConfig getConfig(String name) {

		return instances.get(name);
	}

	public String getServerIp() {
		return serverIp;
	}

	public String getPort() {
		return port;
	}

	public String getIdleTime() {
		return idleTime;
	}

	public String getSotimeout() {
		return sotimeout;
	}

	public String getLocalEncoding() {
		return localEncoding;
	}

	public String getRemoteEncoding() {
		return remoteEncoding;
	}

	public String getGuid() {
		return guid;
	}

	public static void main(String[] args) {

		ClassLoaderConfig config = ClassLoaderConfig.getConfig("crypt");

		System.out.println(config);
	}

}
